package com.itheima.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.EmployeeDao;
import com.itheima.EmployeeService01;
import com.itheima.bean.Employee;
import com.itheima.bean.Params;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import java.time.LocalDateTime;

@Transactional
@Service
public class EmployeeServiceImpl01 implements EmployeeService01 {
    //1. 注入进来dao
    @Autowired
    private EmployeeDao employeeDao;

    /**
     * 登录功能
     *
     * @param employee
     * @return
     */
    @Override
    public Employee login(Employee employee) {
//        1.创建条件对象
        LambdaQueryWrapper<Employee> qw = new LambdaQueryWrapper<>();
        qw.eq(Employee::getUsername, employee.getUsername());
        String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        qw.eq(Employee::getPassword, password);
        return employeeDao.selectOne(qw);
    }

    @Override
    public int add(Employee employee) {
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        String s = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(s);
        return employeeDao.insert(employee);
    }

    @Override
    public IPage<Employee> selectPage(Params params) {
        int page = params.getPage();
        int pageSize = params.getPageSize();
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(params.getName()!=null,Employee::getName,params.getName());
        Page page1 = new Page(page, pageSize);
        return employeeDao.selectPage(page1, queryWrapper);
    }

    @Override
    public int updata(Employee employee) {
       return employeeDao.updateById(employee);
    }
}
